package controller;

import WebSocket.WebsocketEndPoint;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import modal.RoomMaster;
import modal.UserVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.ActivityService;
import service.VideoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping(value = "/public")
public class PublicController {


    @Autowired
    ActivityService activityService;
    @Autowired
    VideoService videoService;

    /**
     * 获取直播列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getLiveList.do")
    @ResponseBody
    public String getLiveList(HttpServletRequest request, HttpServletResponse response)
    {
        response.setHeader("Cache-Control","no-cache");
        List<RoomMaster> list =activityService.getListLive();
        return JSON.toJSONString(list, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 获取当前在线人数
     * @param roomid
     * @param response
     * @return
     */
    @RequestMapping(value = "/getRoomPeople.do")
    @ResponseBody
    public String getRoomNumber(@RequestParam(value = "roomid") String roomid,HttpServletResponse response){
        response.setContentType("text/plain");
        response.setHeader("Cache-Control","no-cache,no-store");
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONObject jsonObject  = new JSONObject();
        jsonObject.put("room",WebsocketEndPoint.getRoomNumber(roomid));
        return jsonObject.toJSONString();
    }

    @RequestMapping("/getVideoInfo.do")
    @ResponseBody
    public UserVideo getUserVideo(@RequestParam("videoid")String videoid){
        return videoService.getUserVideo(videoid);
    }
}
